package com.demo.io.talk2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author : YU.TAN
 * @date : 2021/11/15 15:51
 */
public class UdpRecviveDemo1 {
    public static void main(String[] args) throws Exception{
        DatagramSocket socket = new DatagramSocket(6666);

        while (true){
            //准备接收数据
            byte[] bytes=new byte[1024];
            DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
            socket.receive(packet);
            byte[] data = packet.getData();
            String receiveData = new String(data, 0, data.length);

            System.out.println(receiveData);

            //断开连接 bye
            if (receiveData.equals("bye")){
                break;
            }
        }
        socket.close();
    }
}
